package com.java110.mall.shop.cmd.shop;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.CmdContextUtils;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.account.AccountBondObjDto;
import com.java110.dto.distributionMode.DistributionModeDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.shop.ShopAuditDto;
import com.java110.intf.acct.IAccountBondObjInnerServiceSMO;
import com.java110.intf.goods.IDistributionModeInnerServiceSMO;
import com.java110.intf.shop.IShopAuditInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.mall.shop.bmo.shop.IUpdateShopBMO;
import com.java110.po.distributionMode.DistributionModePo;
import com.java110.po.shop.ShopPo;
import com.java110.po.shop.ShopAuditPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 修改店铺
 *
 */
@Java110Cmd(serviceCode = "shop.updateShop")
public class UpdateShopCmd extends Cmd {

    @Autowired
    private IUpdateShopBMO updateShopBMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Autowired
    private IAccountBondObjInnerServiceSMO accountBondObjInnerServiceSMOImpl;

    @Autowired
    private IShopAuditInnerServiceSMO shopAuditInnerServiceSMOImpl;

    @Autowired
    private IDistributionModeInnerServiceSMO distributionModeInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "shopLogo", "请求报文中未包含shopLogo");
        Assert.hasKeyAndValue(reqJson, "shopName", "请求报文中未包含shopName");
        Assert.hasKeyAndValue(reqJson, "sendAddress", "请求报文中未包含sendAddress");
        Assert.hasKeyAndValue(reqJson, "returnAddress", "请求报文中未包含returnAddress");
        Assert.hasKeyAndValue(reqJson, "returnPerson", "请求报文中未包含returnPerson");
        Assert.hasKeyAndValue(reqJson, "returnPerson", "请求报文中未包含returnPerson");
        Assert.hasKeyAndValue(reqJson, "shopId", "shopId不能为空");
        Assert.hasKeyAndValue(reqJson, "shopType", "请求报文中未包含店铺类型");

        ShopAuditDto shopAuditDto = new ShopAuditDto();
        shopAuditDto.setShopId(reqJson.getString("shopId"));
        shopAuditDto.setState(ShopDto.STATE_APPLY);
        int count = shopAuditInnerServiceSMOImpl.queryShopAuditsCount(shopAuditDto);
        if (count > 0) {
            throw new CmdException("当前店铺已有待审核信息，审核通过后再修改！");
        }

        ShopDto shopDto = new ShopDto();
        shopDto.setShopName(reqJson.getString("shopName"));
        shopDto.setStatusCd("0");
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);
        if (shopDtos.size() > 1) {
            throw new CmdException("保存失败，店铺名称重复！");
        }
        if (ListUtil.isNull(shopDtos)) {
            return;
        }
        if (!shopDtos.get(0).getShopId().equals(reqJson.getString("shopId"))) {
            throw new CmdException("保存失败，店铺名称重复！");
        }
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        String storeId = CmdContextUtils.getStoreId(context);

        ShopPo shopPo = BeanConvertUtil.covertBean(reqJson, ShopPo.class);
        shopPo.setStoreId(storeId);
        // 判断商铺状态
        dealShopState(shopPo);

        ShopDto shopDto = new ShopDto();
        shopDto.setShopId(reqJson.getString("shopId"));
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);
        Assert.listOnlyOne(shopDtos, "商铺不存在");

        //是否需要审核
        boolean ifAudit = ifAuditShop(shopDtos.get(0), reqJson);
        int flag = 0;
        if (ifAudit) {
            ShopAuditPo shopAuditPo = BeanConvertUtil.covertBean(reqJson, ShopAuditPo.class);
            shopAuditPo.setStoreId(shopDtos.get(0).getStoreId());
            shopAuditPo.setAuditId(GenerateCodeFactory.getGeneratorId("11"));
            shopAuditPo.setState(ShopDto.STATE_APPLY);//全部是新申请
            flag = shopAuditInnerServiceSMOImpl.saveShopAudit(shopAuditPo);
            context.setResponseEntity(ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功，含有需要审核的信息，审核通过后即可展示。"));
            return;
        }
        flag = shopInnerServiceSMOImpl.updateShop(shopPo);
        if (flag < 1) {
            throw new CmdException("修改店铺失败");
        }
    }



    /**
     * 判断是否需要审核
     *
     * @param shopDto
     * @param reqJson
     * @return
     */
    private boolean ifAuditShop(ShopDto shopDto, JSONObject reqJson) {
        String shopName = reqJson.getString("shopName");
        String shopLogo = reqJson.getString("shopLogo");
        if (!shopName.equals(shopDto.getShopName())) {
            return true;
        }
        if (!shopLogo.equals(shopDto.getShopLogo())) {
            return true;
        }
        return false;
    }


    private void dealShopState(ShopPo shopPo) {

        if (!ShopDto.STATE_APPLY.equals(shopPo.getShopId())) {
            return;
        }

        //判断是否缴了保证金
        AccountBondObjDto accountBondObjDto = new AccountBondObjDto();
        accountBondObjDto.setObjId(shopPo.getShopId());
        accountBondObjDto.setState(AccountBondObjDto.STATE_BUY_FINISH);
        List<AccountBondObjDto> accountBondObjDtos = accountBondObjInnerServiceSMOImpl.queryAccountBondObjs(accountBondObjDto);
        if (ListUtil.isNull(accountBondObjDtos)) {
            shopPo.setState(ShopDto.STATE_B);
        } else {
            shopPo.setState(ShopDto.STATE_F);
        }
    }

}
